<?php

/**
 * Created by PhpStorm.
 * User: evan
 * Date: 16/9/1
 * Time: 下午6:46
 */
class Base extends MY_Admin_Controller
{
    public function index()
    {
        $this->load->model('common/model_tag','tag');
        $tagInfo = $this->tag->getAllLists();

        $this->load->model('common/model_tag_cate','cate');
        $cateInfo = $this->cate->getAllLists();

        $data = array(
            'tagInfo' => $tagInfo,
            'cateInfo'    => $cateInfo,
        );

        $this->load->view('admin/tag/base/index',$data);
    }


    public function create()
    {
        $aid = $this->uri->segment(4);

        $this->load->model('common/model_tag','tag');
        $tagInfo = $this->tag->getById($aid);

        $this->load->model('common/model_tag_cate','cate');
        $cateInfo = $this->cate->getAllLists();

        $data = array(
            'tagInfo'  => $tagInfo,
            'cateInfo'     => sort_data($cateInfo),
        );

        $this->load->view('admin/tag/base/create',$data);
    }

    /*
    * 竞拍资料编辑后保存
    *
    * */
    public function save()
    {

        $tid = (int)$this->input->get_post('tid');

        //图片未存入
        $data = array(
            'cid'           =>     (int)$this->input->get_post('cid'),
            'name'          =>     trim($this->input->get_post('name')),
            'status'        =>     (int)$this->input->get_post('status'),
        );

        //更新
        $this->load->model('common/model_tag','tag');
        $this->tag->save($data, $tid);

        $this->load->helper('url');
        redirect(url('admin').'tag/base/index');
    }


    /**
     * 删除道具
     */
    public function delete()
    {
        $response = array('code' => '0', 'msg' => '操作成功');

        do {
            $tid = (int)$this->input->get_post('tid');

            if (!$tid)
            {
                $response = error(180001);
                break;
            }

            $this->load->model('common/model_tag','tag');
            $res = $this->tag->delete(array('tid' => $tid));

            if (!$res)
            {
                $response = error(10080);
                break;
            }
        }while (false);


        $this->json_output($response);
    }


    /*
     *
     * 标签分类
     * */
    public function cate_index()
    {
        $this->load->model('common/model_tag_cate','cate');
        $cateInfo = $this->cate->getAllLists();

        $data = array(
            'cateInfo'  =>   sort_data($cateInfo),
        );

        $this->load->view('admin/tag/base/cate_index',$data);
    }

    public function cate_create()
    {
        $cid = $this->uri->segment(4);

        $this->load->model('common/model_tag_cate','cate');
        $cateInfo = $this->cate->getById($cid);
        $cateAll = $this->cate->getAllLists();

        if($cid){
            //找出当前下面的分类
            $sonInfo = get_son($cateAll,$cid);
            //取出所有分类下包含的下级分类
            foreach($sonInfo as $k=>$v){
                unset($cateAll[$k]);
            }
        }

        $data = array(
            'cateInfo'     => $cateInfo,
            'cateAll'      => sort_data($cateAll),
        );

        $this->load->view('admin/tag/base/cate_create',$data);
    }

    public function cate_save()
    {
        $cid = (int)$this->input->get_post('cid');

        $data = array(
            'name'          =>     trim($this->input->get_post('name')),
            'pid'           =>     (int)$this->input->get_post('pid'),
            'sort'          =>     (int)$this->input->get_post('sort'),
            'status'        =>     (int)$this->input->get_post('status'),
            'by_module'     =>     (int)$this->input->get_post('by_module'),
        );

        //更新
        $this->load->model('common/model_tag_cate','cate');
        $this->cate->save($data, $cid);

        $this->load->helper('url');
        redirect(url('admin').'tag/base/cate_index');
    }

    /**
     * 删除分类
     */
    public function cate_delete()
    {
        $response = array('code' => '0', 'msg' => '删除成功');

        do {
            $cid = (int)$this->input->get_post('cid');

            if (!$cid)
            {
                $response = error(180010);
                break;
            }

            $this->load->model('common/model_tag','tag');
            $item = $this->tag->getOne(array('cid' => $cid));

            if (!empty ($item))
            {
                $response = error(180012);
                break;
            }

            $this->load->model('common/model_tag_cate', 'tag_cate');

            $item = $this->tag_cate->getOne(array('pid' => $cid));
            if (!empty ($item))
            {
                $response = error(180013);
                break;
            }

            $item = $this->tag_cate->delete(array('cid' => $cid));

            if (empty ($item))
            {
                $response = error(10080);
                break;
            }


        }while (false);


        $this->json_output($response);
    }


}